<?php

namespace App\Console\Commands\Cruise;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Models\Cruise\CruiseCheckSelfDetailModel;
use App\Models\Cruise\CruiseCheckSelfTaskModel;
use App\Models\Cruise\CruiseOrderModel;
use App\Models\Cruise\CruiseTemplateModel;
use App\Models\Cruise\ShopSelfCheckNotifyConfigModel;
use App\Models\Shop\ShopModel;
use App\Models\System\SystemNotifyCenterModel;
use App\Remote\Rms\RmsUserRemote;
use App\Service\Common\SmsService;
use App\Service\Cruise\Merchant\ShopSelfCheckNotifyConfigService;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class UpdateShopSelfCheckNotifyDefaultConfig extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cruise:update-shop-self-check-notify-default-config';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '门店自检提醒配置默认配置修复';

    /**
     * @var Carbon null
     */
    protected $carbon = null;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->info('运行开始');
        $defaultConf = ShopSelfCheckNotifyConfigModel::getDefaultConfig();
        ShopSelfCheckNotifyConfigModel::query()->chunk(500, function (Collection $modelList) use ($defaultConf) {
            $modelList->each(function (ShopSelfCheckNotifyConfigModel $model) use ($defaultConf) {
                $canSave = false;
                $origin  = [
                    'weekNotifyTimeMap'  => $model->weekNotifyTimeMap,
                    'monthNotifyTimeMap' => $model->monthNotifyTimeMap
                ];
                if (!in_array(
                    $model->weekNotifyTimeMap,
                    array_keys(ShopSelfCheckNotifyConfigModel::NOTIFY_TIME_WEEKEND_DESC_MAP)
                )) {
                    $model->weekNotifyTimeMap = $defaultConf['weekNotifyTimeMap'];
                    $canSave                  = true;
                }

                if (!in_array(
                    $model->monthNotifyTimeMap,
                    array_keys(ShopSelfCheckNotifyConfigModel::NOTIFY_TIME_MONTH_DESC_MAP)
                )) {
                    $model->monthNotifyTimeMap = $defaultConf['monthNotifyTimeMap'];
                    $canSave                   = true;
                }

                if ($canSave) {
                    $model->save();
                    $log = vsprintf("变更门店%s数据  week:%s -> %s ; month:%s -> %s", [
                        $model->shopId,
                        $origin['weekNotifyTimeMap'],
                        $model->weekNotifyTimeMap,
                        $origin['monthNotifyTimeMap'],
                        $model->monthNotifyTimeMap,
                    ]);
                    $this->info($log);
                }
            });
        });
        $this->info('运行结束');
    }
}
